﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;
namespace BCNPCObject.EXCEL
{
    public class cls_HDG_HOP_DONG_Controller
    {
        #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private HDG_HOP_DONG info;

        #endregion

        #region   Constructor

        public cls_HDG_HOP_DONG_Controller()
        {
            info = new HDG_HOP_DONG();
        }
        public cls_HDG_HOP_DONG_Controller(HDG_HOP_DONG Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public HDG_HOP_DONG pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region Methods
        public bool insert_HDG_HOP_DONG(ref long id_HDG_HOP_DONG, ref string maloi)
        {
            long _ID = CMIS2.DB.ExecuteCommand("Select SEQ_HDG_HOP_DONG.NEXTVAL from dual");
            if (_ID == -1) return false;
            try
            {
                info.ID_HDONG = Convert.ToInt64(_ID);
                HDG_HOP_DONG objHDG_HOP_DONG = new HDG_HOP_DONG()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    ID_HDONG = info.ID_HDONG,
                    MA_HDONG = info.MA_HDONG,
                    MA_YCAU_KNAI = info.MA_YCAU_KNAI,
                    MA_LOAIHD = info.MA_LOAIHD,
                    NGAYKY_HDONG = info.NGAYKY_HDONG,
                    NGAY_HLUC = info.NGAY_HLUC,
                    TRANG_THAI = info.TRANG_THAI,
                    HLUC_TUNGAY = info.HLUC_TUNGAY,
                    HLUC_DENNGAY = info.HLUC_DENNGAY,
                    MA_LDO_TLY = info.MA_LDO_TLY,
                    THANG = info.THANG,
                    NAM = info.NAM,
                    SONAM_HLUC = info.SONAM_HLUC,
                    STIEN_DCOC = info.STIEN_DCOC,
                    KWH_TDUONG = info.KWH_TDUONG,
                    TGIAN_TDUONG = info.TGIAN_TDUONG,
                    LOAI_TIEN = info.LOAI_TIEN,
                    DIEN_AP = info.DIEN_AP,
                    TBAO_SUCO = info.TBAO_SUCO,
                    DAP_DDONG = info.DAP_DDONG,
                    TAN_SO = info.TAN_SO,
                    TANSO_SAISO = info.TANSO_SAISO,
                    NGAY_GCS = info.NGAY_GCS,
                    SOLAN_TTOAN = info.SOLAN_TTOAN,
                    THOIHAN_TTOAN = info.THOIHAN_TTOAN,
                    SNAM_GHAN = info.SNAM_GHAN,
                    DCHI_TTOAN = info.DCHI_TTOAN,
                    FILE_DKEM = info.FILE_DKEM,
                    TTHUAN_KHAC = info.TTHUAN_KHAC,
                    NHAN_HDON = info.NHAN_HDON,
                    MA_HTHUC_GHDON = info.MA_HTHUC_GHDON,
                    HTHUC_TBAO = info.HTHUC_TBAO,
                    CQUAN_TRTAI = info.CQUAN_TRTAI,
                    TLEPHAT_BMUA = info.TLEPHAT_BMUA,
                    TLEPHAT_BBAN = info.TLEPHAT_BBAN,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_TAO = info.NGAY_TAO,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    NGUOI_SUA = info.NGUOI_SUA
                };
                CMIS2.DB.HDG_HOP_DONG.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                id_HDG_HOP_DONG = _ID;
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }
        public bool delete_HDG_HOP_DONG(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        var query = from p in CMIS2.DB.HDG_HOP_DONG
                                    where (p.MA_DVIQLY == dt.Rows[i]["MA_DVIQLY"].ToString() && p.ID_HDONG == Convert.ToInt64(dt.Rows[i]["ID_HDONG"]) && p.NGAY_HLUC == Convert.ToDateTime(dt.Rows[i]["NGAY_HLUC"]) && p.TRANG_THAI == 1)
                                    select p;
                        if (query != null && query.Count() > 0)
                        {
                            CMIS2.DB.HDG_HOP_DONG.DeleteAllOnSubmit(query.ToList());
                            CMIS2.DB.SubmitChanges();
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        #endregion
    }
}
